package com.geek.zeus.demos.web.mq.producer;

import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: geek
 * @Date: 2025/10/11/17:22
 * @Description:
 */
@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

//    @Autowired
//    private AmqpAdmin amqpAdmin;
//
//    @PostConstruct
//    public void init() {
//        // 确保队列存在
//        amqpAdmin.declareQueue(new Queue("directqueue", true));
//    }


    @ApiOperation(value="direct发送接口",notes="直接发送到队列。task模式")
    @GetMapping(value="/directSend")
    public Object directSend(String message) throws AmqpException, UnsupportedEncodingException {
        //设置部分请求参数
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        messageProperties.setPriority(2);
        //设置消息转换器，如json
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        //将对象转换成json再发送。
//		rabbitTemplate.convertandsend("",Object);
        //发消息
        rabbitTemplate.send("directqueue",new Message(message.getBytes("UTF-8"),messageProperties));
        return "message sended : "+message;
    }

}
